package com.m.IO.Question;

import java.io.*;

/**
 * 用FileOutputStream在当前目录下创建一个文件“test.txt”,
 * 并向文件中输入“hello world”,如果文件存在，则向原文件内容后面追加
 * 用FileInputStream读入test.txt文件，并在控制台打印出text.txt中的内容
 * 要求用try-catch-finally处理异常，并且关闭流应该放在finally块中
 *
 *
 * 注：因为向原文件中进行内容追加，每次结果会有所不同
 * @author Mc.Chai
 * @program: Java基础
 * @create 2021-11-22-20:22 20:22
 */
public class TestQuestion8 {
    public static void main(String[] args) {
        try {
            FileOutputStream fileOutputStream=new FileOutputStream("test.txt",true);
            byte[] b=new byte[]{'h','e','l','l','o','w','o','r','l','d'};
            fileOutputStream.write(b);

            FileInputStream fileInputStream=new FileInputStream("test.txt");
            byte[] cache=new byte[4];
            System.out.println((char)fileInputStream.read());
            while (true){
                int n=fileInputStream.read(cache);
                if (n==-1) break;
                for (int i = 0; i < n; i++) {
                    System.out.print((char) cache[i]);
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

        }
    }
}
//15
